import unittest
from AStock.ASCrawler import (
    ASFinancialCrawler,
    ASFinancialSaver,
    ASFinancialNoticeDateCrawler,
    ASFinancialNoticeDateSaver,
    ASResearchReportCrawler,
    ASResearchReportSaver,
    ASPePercentageSaver,
    ASPePercentageCrawler,
    ASFinancialPreviewSaver,
    ASFinancialPreviewCrawler,
    ASStockDayCrawler,
    ASStockDaySaver
)
import random
from datetime import datetime


class TestFinancial(unittest.TestCase):

    def testFinancialCrawlerAndSaver(self):
        event_type = ['cwbl', 'zcfzb', 'lrb', 'xjllb']
        for et in event_type:
            event_data = {
                'event_type': et,
                'code': '603707',
                'report_date': '20200930'
            }
            crawler = ASFinancialCrawler()
            res = crawler.run(event_data)
            print('ASFinancialCrawler result: {}'.format(res))
            self.assertTrue('df' in res)

            saver = ASFinancialSaver()
            res = saver.run(res)
            print('ASFinancialSaver result: {}'.format(res))
            self.assertTrue('succ' in res)

    def testFinancialNoticeDateCrawlerAndSaver(self):
        event_type = 'financial_notice_date'
        event_data = {
            'event_type': event_type,
            'report_date': '20200630',
            'page_no': 1,
            'page_size': 50,
            'rand': random.randint(50000000, 57000000)
        }
        while True:
            crawler = ASFinancialNoticeDateCrawler()
            res = crawler.run(event_data)
            print('ASFinancialNoticeDateCrawler result: {}'.format(res))
            self.assertTrue('error' not in res and 'pages' in res and 'data' in res)

            # save
            saver = ASFinancialNoticeDateSaver()
            event_data_save = {
                'event_type': event_type,
                'report_date': '20200630',
                'data': res['data']
            }
            res_save = saver.run(event_data_save)
            print('ASFinancialSaver result: {}'.format(res_save))
            self.assertTrue('succ' in res_save)

            if event_data['page_no'] >= res['pages']:
                break

            event_data['page_no'] += 1
            event_data['rand'] += 1


class TestResearchReport(unittest.TestCase):

    def testResearchReportCrawlerAndSaver(self):
        event_type = 'research_report'

        event_data = {
            'event_type': event_type,
            'begin_date': '20200916',
            'end_date': '20200917',
            'page_no': 1,
            'page_size': 10,
            'rand': random.randint(50000000, 57000000),
            'timestamp': int(datetime.now().timestamp())
        }
        crawler = ASResearchReportCrawler()
        res = crawler.run(event_data)
        print('ASResearchReportCrawler result: {}'.format(res))
        self.assertTrue('data' in res)
        self.assertTrue('page_no' in res)
        self.assertTrue('pages' in res)

        saver = ASResearchReportSaver()
        res = saver.run({'event_type': event_type, 'data': res['data']})
        print('ASFinancialSaver result: {}'.format(res))
        self.assertTrue('succ' in res)


class TestPePercentage(unittest.TestCase):

    def testPePercentageCrawlerAndSaver(self):
        event_type = 'pe_percentage'

        event_data = {
            'event_type': event_type,
            'code': '002511'
        }
        crawler = ASPePercentageCrawler()
        res = crawler.run(event_data)
        print('ASPePercentageCrawler result: {}'.format(res))
        self.assertTrue('code' in res)
        self.assertTrue('pe30' in res)
        self.assertTrue('pe50' in res)
        self.assertTrue('pe70' in res)

        saver = ASPePercentageSaver()
        event_data = {'event_type': event_type}
        event_data.update(res)
        res = saver.run(event_data)
        print('ASPePercentageSaver result: {}'.format(res))
        self.assertTrue('succ' in res)


class TestFinancialPreview(unittest.TestCase):

    def testFinancialPreviewCrawlerAndSaver(self):
        event_type = ['yjyg', 'yjkb']
        for et in event_type:
            event_data = {
                'event_type': et,
                'report_date': '20200930',
                'page_no': 1,
                'page_size': 100,
                'rand': random.randint(50000000, 57000000)
            }
            while True:
                crawler = ASFinancialPreviewCrawler()
                res = crawler.run(event_data)
                print('ASFinancialPreviewCrawler result: {}'.format(res))
                self.assertTrue('error' not in res and 'pages' in res and 'data' in res)

                # save
                saver = ASFinancialPreviewSaver()
                event_data_save = {
                    'event_type': et,
                    'report_date': '20200930',
                    'data': res['data']
                }
                res_save = saver.run(event_data_save)
                print('ASFinancialPreviewSaver result: {}'.format(res_save))
                self.assertTrue('succ' in res_save)

                if event_data['page_no'] >= res['pages']:
                    break

                event_data['page_no'] += 1
                event_data['rand'] += 1


class TestStockDay(unittest.TestCase):

    def testStockDayCrawlerAndSaver(self):
        event_data = {
            'event_type': 'stock_day',
            'code': '300003'
        }
        crawler = ASStockDayCrawler()
        res = crawler.run(event_data)
        print('ASStockDayCrawler result: {}'.format(res))
        self.assertTrue('code' in res)
        self.assertTrue('date' in res)
        self.assertTrue('open' in res)
        self.assertTrue('pre_close' in res)

        saver = ASStockDaySaver()
        event_data = {'event_type': 'stock_day', 'data': res}
        res = saver.run(event_data)
        print('ASStockDaySaver result: {}'.format(res))
        self.assertTrue('succ' in res)